package fudan.se.project.exception;

import com.fasterxml.jackson.core.JsonParseException;
import fudan.se.project.controller.response.BaseResponse;
import fudan.se.project.exception.file.FileNotFoundException;
import fudan.se.project.util.http.ResponseWrapperUtil;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

/**
 * @author Gao Longchao
 */
@RestControllerAdvice
public class ControllerAdvisor extends ResponseEntityExceptionHandler {

    private final ResponseWrapperUtil responseWrapperUtil;

    public ControllerAdvisor(ResponseWrapperUtil responseWrapperUtil) {
        this.responseWrapperUtil = responseWrapperUtil;
    }


    @Override
    protected ResponseEntity<Object> handleMethodArgumentNotValid(
            MethodArgumentNotValidException ex,
            HttpHeaders headers, HttpStatus status, WebRequest request) {
        BaseResponse response = new BaseResponse("Arguments Error: 参数错误");
        return responseWrapperUtil.buildResponseBody(response, HttpStatus.NOT_ACCEPTABLE);
    }

    @Override
    protected ResponseEntity<Object> handleMissingServletRequestParameter(
            MissingServletRequestParameterException ex,
            HttpHeaders headers, HttpStatus status, WebRequest request) {
        BaseResponse response = new BaseResponse("Arguments Error: 参数错误");
        return responseWrapperUtil.buildResponseBody(response, HttpStatus.NOT_ACCEPTABLE);
    }

    @ExceptionHandler(JsonParseException.class)
    public ResponseEntity<Object> handleJsonParse(
            JsonParseException ex,
            HttpHeaders headers, HttpStatus status, WebRequest request) {
        BaseResponse response = new BaseResponse("Json Parse Error: json转换错误");
        return responseWrapperUtil.buildResponseBody(response, HttpStatus.BAD_REQUEST);
    }

    @Override
    public ResponseEntity<Object> handleHttpMessageNotReadable(
            HttpMessageNotReadableException ex,
            HttpHeaders headers, HttpStatus status, WebRequest request) {
        BaseResponse response = new BaseResponse("Not Readable: json不可读");
        return responseWrapperUtil.buildResponseBody(response, HttpStatus.BAD_REQUEST);
    }

    @ExceptionHandler(FileNotFoundException.class)
    public ResponseEntity<Object> handleFileNotFound(
            FileNotFoundException ex,
            HttpHeaders headers, HttpStatus status, WebRequest request) {
        BaseResponse response = new BaseResponse("Not Found: 资源不存在");
        return responseWrapperUtil.buildResponseBody(response, HttpStatus.NOT_FOUND);
    }

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public ResponseEntity<Object> handleMaxUploadSizeExceeded(
            MaxUploadSizeExceededException ex) {
        BaseResponse response = new BaseResponse("File Size Error: 文件大小不得超过 20 mb");
        return responseWrapperUtil.buildResponseBody(response, HttpStatus.NOT_ACCEPTABLE);
    }

}
